package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;

import network.Envio;
import network.Portas;

import listeners.EnviarListener;
import listeners.ErroListener;
import listeners.MenuListener;
import listeners.PauseListener;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Main extends JFrame {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JMenuBar jMenuBar;
	private JMenu jMenu1;
	private JMenuItem jMenuItemPropriedades;
	private static JTextArea jTextAreaConsole;
	private JMenuItem jMenuItemReset;
	private JPanel jPanelPrincipal;
	private JPanel jPanelMatriz;
	private JPanel jPanelConsole;
	private JButton jButtonInit;
	private JButton jButtonSimulacao;
	private JButton jButtonPause;
	private JMenuItem jMenuItemSair;
	private JMenuItem jMenuItemCarregar;

	public JMenuItem getjMenuItemCarregar() {
		return jMenuItemCarregar;
	}

	private static JPanel[][] Matriz = new JPanel[20][20];
	private int numPacotes = 0;

	public static void pintarMatriz(int i, int j, Color c) {
		Matriz[i][j].setBackground(c);
	}

	private void initGUI() {
		try {
			{
				this.setResizable(false);
				this.setDefaultCloseOperation(EXIT_ON_CLOSE);
				this.setSize(800, 600);
				this.setTitle("Trabalho de Redes NP1");
				GroupLayout thisLayout = new GroupLayout(
						(JComponent) getContentPane());
				getContentPane().setLayout(thisLayout);
				this.setLocationRelativeTo(null);
				{
					jPanelPrincipal = new JPanel();
					GroupLayout jPanelPrincipalLayout = new GroupLayout(
							(JComponent) jPanelPrincipal);
					jPanelPrincipal.setLayout(jPanelPrincipalLayout);
					{
						jPanelMatriz = new JPanel();
						GridLayout jPanelMatrizLayout = new GridLayout(20, 20);
						jPanelMatrizLayout.setColumns(20);
						jPanelMatrizLayout.setHgap(8);
						jPanelMatrizLayout.setVgap(5);
						jPanelMatrizLayout.setRows(20);
						jPanelMatriz.setLayout(jPanelMatrizLayout);
						jPanelMatriz.setBorder(new SoftBevelBorder(
								BevelBorder.LOWERED, null, null, null, null));
						jPanelMatriz.setBackground(new java.awt.Color(128, 128,
								128));
					}
					{
						jPanelConsole = new JPanel();
						jPanelConsole.setBorder(new SoftBevelBorder(
								BevelBorder.LOWERED, null, null, null, null));
						jPanelConsole.setBackground(new java.awt.Color(255,
								255, 255));
						{
							jTextAreaConsole = new JTextArea();
							jPanelConsole.add(jTextAreaConsole);
							jTextAreaConsole
									.setPreferredSize(new java.awt.Dimension(
											647, 199));
						}

					}
					{
						jButtonInit = new JButton();
						jButtonInit.setText("Iniciar");
						jButtonInit.addActionListener(new EnviarListener(this));
					}
					{
						jButtonPause = new JButton();
						jButtonPause.setText("Pausar");
						jButtonPause.addActionListener(new PauseListener(this));
					}
					{
						jButtonSimulacao = new JButton();
						jButtonSimulacao.setText("Simular Erro");
						jButtonSimulacao.addActionListener(new ErroListener(
								this));
					}
					jPanelPrincipalLayout
							.setHorizontalGroup(jPanelPrincipalLayout
									.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											jPanelPrincipalLayout
													.createParallelGroup()
													.addComponent(
															jPanelMatriz,
															GroupLayout.Alignment.LEADING,
															GroupLayout.PREFERRED_SIZE,
															658,
															GroupLayout.PREFERRED_SIZE)
													.addComponent(
															jPanelConsole,
															GroupLayout.Alignment.LEADING,
															GroupLayout.PREFERRED_SIZE,
															658,
															GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(
											LayoutStyle.ComponentPlacement.RELATED)
									.addGroup(
											jPanelPrincipalLayout
													.createParallelGroup()
													.addComponent(
															jButtonInit,
															GroupLayout.Alignment.LEADING,
															0, 100,
															Short.MAX_VALUE)
													.addComponent(
															jButtonPause,
															GroupLayout.Alignment.LEADING,
															0, 100,
															Short.MAX_VALUE)
													.addComponent(
															jButtonSimulacao,
															GroupLayout.Alignment.LEADING,
															0, 100,
															Short.MAX_VALUE))
									.addContainerGap());
					jPanelPrincipalLayout
							.setVerticalGroup(jPanelPrincipalLayout
									.createSequentialGroup()
									.addContainerGap()
									.addGroup(
											jPanelPrincipalLayout
													.createParallelGroup()
													.addComponent(
															jPanelMatriz,
															GroupLayout.Alignment.LEADING,
															GroupLayout.PREFERRED_SIZE,
															317,
															GroupLayout.PREFERRED_SIZE)
													.addGroup(
															GroupLayout.Alignment.LEADING,
															jPanelPrincipalLayout
																	.createSequentialGroup()
																	.addComponent(
																			jButtonInit,
																			GroupLayout.PREFERRED_SIZE,
																			GroupLayout.PREFERRED_SIZE,
																			GroupLayout.PREFERRED_SIZE)
																	.addGap(24)
																	.addComponent(
																			jButtonPause,
																			GroupLayout.PREFERRED_SIZE,
																			GroupLayout.PREFERRED_SIZE,
																			GroupLayout.PREFERRED_SIZE)
																	.addGap(24)
																	.addComponent(
																			jButtonSimulacao,
																			GroupLayout.PREFERRED_SIZE,
																			23,
																			GroupLayout.PREFERRED_SIZE)
																	.addGap(200)))
									.addPreferredGap(
											LayoutStyle.ComponentPlacement.RELATED)
									.addComponent(jPanelConsole, 0, 196,
											Short.MAX_VALUE).addContainerGap());
				}
				thisLayout
						.setVerticalGroup(thisLayout.createSequentialGroup()
								.addComponent(jPanelPrincipal, 0, 541,
										Short.MAX_VALUE));
				thisLayout.setHorizontalGroup(thisLayout
						.createSequentialGroup().addComponent(jPanelPrincipal,
								0, 784, Short.MAX_VALUE));
				{
					jMenuBar = new JMenuBar();
					setJMenuBar(jMenuBar);
					{
						jMenu1 = new JMenu();
						jMenuBar.add(jMenu1);
						jMenu1.setText("Arquivo");
						{
							jMenuItemCarregar = new JMenuItem();
							jMenu1.add(jMenuItemCarregar);
							jMenuItemCarregar.setText("Carregar Arquivo");
							jMenuItemCarregar
									.addActionListener(new MenuListener(this,
											jMenuItemCarregar));
						}
						{
							jMenuItemReset = new JMenuItem();
							jMenu1.add(jMenuItemReset);
							jMenuItemReset.setText("Reset");
							jMenuItemReset.addActionListener(new MenuListener(
									this, jMenuItemReset));
						}
						{
							jMenuItemPropriedades = new JMenuItem();
							jMenu1.add(jMenuItemPropriedades);
							jMenuItemPropriedades.setText("Propriedades");
							jMenuItemPropriedades.addActionListener(new MenuListener(this, jMenuItemPropriedades));
						}
						{
							jMenuItemSair = new JMenuItem();
							jMenu1.add(jMenuItemSair);
							jMenuItemSair.setText("Sair");
							jMenuItemSair.addActionListener(new MenuListener(
									this, jMenuItemSair));
						}
						{
							construirMatriz();
							this.setVisible(true);
						}

					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Main().initGUI();
		Portas.abrirCom1();
	}

	public void construirMatriz() {
		JPanel aux;
		for (int i = 0; i < 20; i++) {
			for (int j = 0; j < 20; j++) {
				aux = new JPanel();
				aux.setBackground(Color.white);
				Matriz[i][j] = aux;
				jPanelMatriz.add(aux);
			}
		}
		jPanelMatriz.setBackground(Color.white);
	}

	public void preencherMatriz(int tamanho) {
		int i = 0;
		int j = 0;
		while (tamanho > 0) {
			Matriz[i][j].setBackground(Color.blue);
			Matriz[i][j].setBorder(new LineBorder(Color.black));
			if (j < 20) {
				j++;
			} else {
				j = 0;
				i++;
			}
			tamanho--;
		}
		jPanelMatriz.updateUI();
	}

	public static void printConsole(String mensagem) {
		jTextAreaConsole.setText(jTextAreaConsole.getText() + "\n" + mensagem);
	}

	public JMenuItem getjMenuItemPropriedades() {
		return jMenuItemPropriedades;
	}

}
